package 栈;

/**
 * @author 陈艺博
 * @date 2021-02-20 21:51
 * @description https://leetcode-cn.com/problems/evaluate-reverse-polish-notation/
 **/
public class _150_evaluate_reverse_polish_notation_逆波兰表达式求值 {
    public static void main(String[] args) {
        System.out.println(evalRPN(new String[]{"2", "1", "+", "3", "*"}));
    }

    static public int evalRPN(String[] tokens) {
        int[] stack = new int[tokens.length];
        int top = -1;
        for (String token : tokens) {
            switch (token) {
                case "+":
                    stack[--top] += stack[top + 1];
                    break;

                case "-":
                    stack[--top] -= stack[top + 1];
                    break;

                case "*":
                    stack[--top] *= stack[top + 1];
                    break;

                case "/":
                    stack[--top] /= stack[top + 1];
                    break;
                default:
                    stack[++top] = Integer.parseInt(token);
                    break;
            }
        }
        return stack[top];
    }
}
